package com.lcp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcp.entity.TcmKnowledge;
import com.lcp.mapper.TcmKnowledgeMapper;
import com.lcp.service.TcmKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class TcmKnowledgeServiceImpl implements TcmKnowledgeService {

    @Autowired
    private TcmKnowledgeMapper tcmKnowledgeMapper;

    @Override
    public TcmKnowledge getById(Long id) {
        return tcmKnowledgeMapper.selectById(id);
    }

    @Override
    public IPage<TcmKnowledge> page(Integer pageNum, Integer pageSize, String category, String keyword) {
        QueryWrapper<TcmKnowledge> wrapper = new QueryWrapper<>();
        if (category != null && !category.isEmpty()) {
            wrapper.eq("category", category);
        }
        if (keyword != null && !keyword.isEmpty()) {
            wrapper.and(w -> w.like("title", keyword)
                    .or().like("content", keyword));
        }
        wrapper.orderByDesc("publish_time");
        return tcmKnowledgeMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    @Transactional
    public TcmKnowledge create(TcmKnowledge knowledge) {
        knowledge.setCreateTime(LocalDateTime.now());
        knowledge.setUpdateTime(LocalDateTime.now());
        tcmKnowledgeMapper.insert(knowledge);
        return knowledge;
    }

    @Override
    @Transactional
    public TcmKnowledge update(TcmKnowledge knowledge) {
        knowledge.setUpdateTime(LocalDateTime.now());
        tcmKnowledgeMapper.updateById(knowledge);
        return knowledge;
    }

    @Override
    @Transactional
    public void delete(Long id) {
        tcmKnowledgeMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void incrementViewCount(Long id) {
        TcmKnowledge knowledge = tcmKnowledgeMapper.selectById(id);
        if (knowledge != null) {
            knowledge.setViewCount(knowledge.getViewCount() + 1);
            tcmKnowledgeMapper.updateById(knowledge);
        }
    }

    @Override
    @Transactional
    public void incrementLikeCount(Long id) {
        TcmKnowledge knowledge = tcmKnowledgeMapper.selectById(id);
        if (knowledge != null) {
            knowledge.setLikeCount(knowledge.getLikeCount() + 1);
            tcmKnowledgeMapper.updateById(knowledge);
        }
    }

    @Override
    public List<Map<String, Object>> getCategoryStatistics() {
        QueryWrapper<TcmKnowledge> wrapper = new QueryWrapper<>();
        wrapper.select("category", "count(*) as count").groupBy("category");
        return tcmKnowledgeMapper.selectMaps(wrapper);
    }

    @Override
    public List<TcmKnowledge> getHotKnowledge(Integer limit) {
        QueryWrapper<TcmKnowledge> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count").last("limit " + limit);
        return tcmKnowledgeMapper.selectList(wrapper);
    }

    @Override
    public List<TcmKnowledge> getRelatedKnowledge(Long id, Integer limit) {
        TcmKnowledge current = tcmKnowledgeMapper.selectById(id);
        if (current == null) return Collections.emptyList(); // 修改此处
        QueryWrapper<TcmKnowledge> wrapper = new QueryWrapper<>();
        wrapper.eq("category", current.getCategory())
                .ne("id", id)
                .orderByDesc("publish_time")
                .last("limit " + limit);
        return tcmKnowledgeMapper.selectList(wrapper);
    }

}
